package com.haima.ziliao.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.haima.common.annotation.RepeatSubmit;
import com.haima.common.core.domain.entity.SysUser;
import com.haima.common.utils.SecurityUtils;
import com.haima.system.service.ISysUserService;
import com.haima.ziliao.domain.PNianji;
import com.haima.ziliao.domain.PXueke;
import com.haima.ziliao.service.IPNianjiService;
import com.haima.ziliao.service.IPXuekeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PJiazhang;
import com.haima.ziliao.service.IPJiazhangService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 家长需求Controller
 * 
 * @author hima
 * @date 2024-12-26
 */
@RestController
@RequestMapping("/ziliao/jiazhang")
public class PJiazhangController extends BaseController
{
    @Autowired
    private IPJiazhangService pJiazhangService;
    @Autowired
    private IPNianjiService nianjiService;
    @Autowired
    private IPXuekeService xuekeService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询家长需求列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:jiazhang:list')")
    @GetMapping("/list")
    public TableDataInfo list(PJiazhang pJiazhang)
    {
        startPage();
        List<Map<String,Object>> list = pJiazhangService.selectPJiazhangListMap(pJiazhang);
        for(int i=0;i<list.size();i++){
            Long xkid = (Long) list.get(i).get("xueke");
            Long njid = (Long) list.get(i).get("nianji");

            PNianji nianji = nianjiService.selectPNianjiById(njid);
            PXueke xueke = xuekeService.selectPXuekeById(xkid);
            list.get(i).put("njname",nianji.getName());
            list.get(i).put("xkname",xueke.getName());
        }
        return getDataTable(list);
    }

    @GetMapping("/addData")
    @RepeatSubmit
    public AjaxResult addData(PJiazhang pJiazhang)
    {
        pJiazhang.setUid(SecurityUtils.getUserId());
        SysUser su = userService.selectUserById(SecurityUtils.getUserId());
        pJiazhang.setAvapic(su.getAvatar());
        pJiazhang.setStatus("1");//待审核
        pJiazhangService.insertPJiazhang(pJiazhang);
        return AjaxResult.success("ok");
    }

    @GetMapping("/getList")
    public TableDataInfo getList(PJiazhang pJiazhang)
    {
        pJiazhang.setStatus("2");
        startPage();
        List<Map<String,Object>> list = pJiazhangService.selectPJiazhangListMap(pJiazhang);
        for(int i=0;i<list.size();i++){
            Long xkid = (Long) list.get(i).get("xueke");
            Long njid = (Long) list.get(i).get("nianji");

            PNianji nianji = nianjiService.selectPNianjiById(njid);
            PXueke xueke = xuekeService.selectPXuekeById(xkid);
            list.get(i).put("njname",nianji.getName());
            list.get(i).put("xkname",xueke.getName());
        }
        return getDataTable(list);
    }

    @GetMapping("/getDetail")
    public AjaxResult getDetail(PJiazhang pJiazhang)
    {
        PJiazhang jiazhang = pJiazhangService.selectPJiazhangById(pJiazhang.getId());
        return AjaxResult.success(jiazhang);
    }

    @GetMapping("/getListForMe")
    public TableDataInfo getListForMe(PJiazhang pJiazhang)
    {
        pJiazhang.setUid(SecurityUtils.getUserId());
        startPage();
        List<PJiazhang> list = pJiazhangService.selectPJiazhangList(pJiazhang);
        for(int i=0;i<list.size();i++){
            Long xkid = list.get(i).getXueke();
            Long njid = list.get(i).getNianji();

            PNianji nianji = nianjiService.selectPNianjiById(njid);
            PXueke xueke = xuekeService.selectPXuekeById(xkid);
            list.get(i).setNjname(nianji.getName());
            list.get(i).setXkname(xueke.getName());
        }
        return getDataTable(list);
    }

    @GetMapping("/xiajia")
    @RepeatSubmit
    public AjaxResult xiajia(PJiazhang pJiazhang)
    {
        PJiazhang jiazhang = pJiazhangService.selectPJiazhangById(pJiazhang.getId());
        if(jiazhang.getUid().equals(SecurityUtils.getUserId())){
            jiazhang.setStatus("3");//下架
        }
        pJiazhangService.updatePJiazhang(jiazhang);
        return AjaxResult.success("ok");
    }

    @GetMapping("/shangjia")
    @RepeatSubmit
    public AjaxResult shangjia(PJiazhang pJiazhang)
    {
        PJiazhang jiazhang = pJiazhangService.selectPJiazhangById(pJiazhang.getId());
        if(jiazhang.getUid().equals(SecurityUtils.getUserId())){
            jiazhang.setStatus("2");//下架
        }
        pJiazhangService.updatePJiazhang(jiazhang);
        return AjaxResult.success("ok");
    }

    @GetMapping(value = "/shanchu")
    @RepeatSubmit
    public AjaxResult shanchu(Long id)
    {
        PJiazhang jiazhang = pJiazhangService.selectPJiazhangById(id);
        if(jiazhang.getUid().equals(SecurityUtils.getUserId())){
            return success(pJiazhangService.deletePJiazhangById(id));
        }else{
            return success();
        }
    }

    @GetMapping("/updateData")
    @RepeatSubmit
    public AjaxResult updateData(PJiazhang pJiazhang)
    {
        pJiazhang.setStatus("1");
        return toAjax(pJiazhangService.updatePJiazhang(pJiazhang));
    }

    /**
     * 导出家长需求列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:jiazhang:export')")
    @Log(title = "家长需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PJiazhang pJiazhang)
    {
        List<PJiazhang> list = pJiazhangService.selectPJiazhangList(pJiazhang);
        ExcelUtil<PJiazhang> util = new ExcelUtil<PJiazhang>(PJiazhang.class);
        util.exportExcel(response, list, "家长需求数据");
    }

    /**
     * 获取家长需求详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:jiazhang:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pJiazhangService.selectPJiazhangById(id));
    }

    /**
     * 新增家长需求
     */
    @PreAuthorize("@ss.hasPermi('ziliao:jiazhang:add')")
    @Log(title = "家长需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PJiazhang pJiazhang)
    {
        return toAjax(pJiazhangService.insertPJiazhang(pJiazhang));
    }

    /**
     * 修改家长需求
     */
    @PreAuthorize("@ss.hasPermi('ziliao:jiazhang:edit')")
    @Log(title = "家长需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PJiazhang pJiazhang)
    {
        return toAjax(pJiazhangService.updatePJiazhang(pJiazhang));
    }

    /**
     * 删除家长需求
     */
    @PreAuthorize("@ss.hasPermi('ziliao:jiazhang:remove')")
    @Log(title = "家长需求", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pJiazhangService.deletePJiazhangByIds(ids));
    }
}
